//
//  ViewController.m
//  15-UICollectionViewLifeCycle
//
//  Created by 曹曹 on 16/8/3.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"

#define kCollectionViewReuse @"cellReuse"
#define kArc4RandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (CGRectGetWidth(self.view.bounds) - 15) / 2;
    layout.itemSize = CGSizeMake(width, 120);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    // 关于CollectionView LifeCycle
    // 1.numberOfSectionsInCollectionView
    // 2.collectionView:numberOfItemsInSection:
    // 注意: 3-4循环执行
    // 3.prepareForReuse(从重用池取出时调用)
    // 4.collectionView:cellForItemAtIndexPath:
    // 5.collectionView:willDisplayCell:
    // 6.当cell完全出屏幕时, collectionView:didEndDisplayCell:
    
    // iOS 10以前
    // 出屏幕立即返回, 依旧进入3 -> 4 -> 5
    // iOS 10以后
    // 出屏幕立即返回, 会执行4 -> 5, 不会执行3, 提高了效率
}
#pragma mark - collectionView lifeCycle
// 区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"[%@----%s]", self, __func__);
    return 1;
}
// 区里Item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"[%@----%s]", self, __func__);
    return 100;
}
// 从重用池取出cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.backgroundColor = kArc4RandomColor;
    NSLog(@"%@----%ld----[%@ : %s]", cell, indexPath.row, self, __func__);
    return cell;
}
// iOS 8.0以后推出, cell将要显示在屏幕上
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld----[%@ : %s]", cell, indexPath.row, self, __func__);
}
// iOS 8.0以后推出, cell已经离开屏幕时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld----[%@ : %s]", cell, indexPath.row, self, __func__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
